package com.by.toolkitspring.config;

import com.by.toolkit.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dynamictp.core.DtpRegistry;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;

import java.util.concurrent.Executor;

/**
 * 异步线程池
 *
 * @author YanBingHao
 * @since 2024/1/15
 */
@Slf4j
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class AsyncExecutorConfig extends AsyncConfigurerSupport {

    public static final String BY_ASYNC_EXECUTOR_POOL_NAME = "byAsyncPool";

    /**
     * 获取平台默认的线程池
     *
     * @author YanBingHao
     * @since 2024/1/15
     */
    @Override
    public Executor getAsyncExecutor() {
        return DtpRegistry.getDtpExecutor(BY_ASYNC_EXECUTOR_POOL_NAME);
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (ex, method, params) -> log.error("[SpringAsyncExceptionHandler][handleUncaughtException] [HasException]:[{}]", ExceptionUtil.getStackTrace(ex));
    }

}
